package com.tsingteng.cosfun.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import android.support.v4.content.PermissionChecker;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.helper.ActivityManagers;
import com.tsingteng.cosfun.helper.DialogHelper;
import com.tsingteng.cosfun.utils.LogUtils;
import com.tsingteng.cosfun.utils.NotchUtil;
import com.tsingteng.cosfun.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by Administrator on 2018/8/2.
 */

public abstract class BaseVideoActivity extends AppCompatActivity {

    private Dialog mHintDialog;
    private PowerManager.WakeLock wl;
    private PowerManager pm;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.i("BaseActivity", getClass().getSimpleName() + " onCreate("
                + (savedInstanceState == null ? "" : savedInstanceState) + ")");

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(getLayoutId());
        getIntentData(getIntent());
        ActivityManagers.getInstance().addShootActivity(this);
        ActivityManagers.getInstance().addFoundActivity(this);
        //管理屏幕常亮状态
        pm = (PowerManager) getSystemService(POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
                getResources().getString(R.string.app_name));
        bright();
        mHintDialog = DialogHelper.getHintDialog(this);
        initView();
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
//                setHideVirtualKey(getWindow());
            }
        });

//        NotchUtil.setWindowTrans(this, true, false);




    }


    /**
     * 屏幕常亮
     */
    public void bright() {
        try {
            wl.acquire();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 取消常亮
     */
    public void cancelBright() {
        try {
            wl.release();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 隐藏虚拟按键
     * @param window
     */
    public void setHideVirtualKey(Window window){
        //保持布局状态
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE|
                //布局位于状态栏下方
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|
                //全屏
                View.SYSTEM_UI_FLAG_FULLSCREEN|
                //隐藏导航栏
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        if (Build.VERSION.SDK_INT>=19){
            uiOptions |= 0x00001000;
        }else{
            uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
        }
        window.getDecorView().setSystemUiVisibility(uiOptions);
    }


    /**
     * 隐藏虚拟按键，并且全屏
     */
    private void hideSystemNavigationBar() {
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
            View view = this.getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.i("BaseActivity", getClass().getSimpleName() + " onResume");
        MobclickAgent.onResume(this);
    }
    @Override
    protected void onPause() {
        super.onPause();
        LogUtils.i("BaseActivity", getClass().getSimpleName() + " onPause");
        MobclickAgent.onPause(this);
    }

    /**
     * 初始化数据
     */
    protected abstract void initView();


    /**
     *得到上个页面传递来的数据
     * @param intent
     */
    protected abstract void getIntentData(Intent intent);

    /**
     * 得到布局文件
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 吐司
     *
     * @param msg
     */
    protected void showToast(String msg) {
        ToastUtils.showToast(msg);
    }

    /**
     * 显示提示消息的提示框
     * @param text
     */
    protected void showHintDialog(String text) {
        if (mHintDialog != null) {
            TextView tv = mHintDialog.findViewById(R.id.tv_load_dialog);
            tv.setText(text);
            mHintDialog.show();
        }
    }

    /**
     * 隐藏加载框
     */
    protected void hideHintDialog() {
        if (mHintDialog != null && mHintDialog.isShowing()) {
            Context context = ((ContextWrapper)mHintDialog.getContext()).getBaseContext();
            if(context instanceof Activity) {
                if (!((Activity)context).isFinishing() && !((Activity)context).isDestroyed()) {
                    mHintDialog.dismiss();
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManagers.getInstance().finishShootActivity(this);
        ActivityManagers.getInstance().finishFoundActivity(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    /**
     * 延迟某个时间执行某个任务
     *
     * @param action        Runnable对象
     * @param delayMillis   延迟的时间
     */
    public boolean postDelayed(Runnable action, long delayMillis) {
        return getWindow().getDecorView().postDelayed(action, delayMillis);
    }

    /**
     * 删除某个延迟任务
     * @param action        Runnable对象
     */
    public boolean removeCallbacks(Runnable action) {
        if(getWindow().getDecorView() != null) {
            return getWindow().getDecorView().removeCallbacks(action);
        }else {
            return true;
        }
    }
}
